﻿//goobol page loader jquery plugin
//Creator:hhan
(function ($) {
    $.goobol = {
        loader: function (parentObj, pageNames) {
            _parentObj = parentObj;
            _pageNames = pageNames;
            $.goobol.pageLoading();
            initialConfig();
            $.goobol.loadCurrentPage();
        },
        loadCurrentPage: function () {
            var pageNamesSP = _pageNames.split(',');
            $(pageNamesSP).each(function () {
                var pages = _config[this].page;
                var goobolUrlKV = _config[this].goobolUrlKeyValue;
                $(pages).each(function (i) {
                    $.ajax({
                        url: pages[i],
                        cache: false,
                        dataType: "html",
                        success: function (data) {
                            var pageObj = $(data);
                            $(_parentObj).html(pageObj[0].outerHTML);
                            $.goobol.parsingGoobolUrls(pageObj, goobolUrlKV);
                            $(_parentObj).html(pageObj[0].outerHTML);
                            $.goobol.loadCurrentPageJs();
                        }
                    });
                });
            });
        },
        loadCurrentPageJs: function () {
            var pageNamesSP = _pageNames.split(',');
            $(pageNamesSP).each(function () {
                var pageJs = _config[this].js;
                $(pageJs).each(function (i) {
                    $.goobol.addNewScriptFile($(_parentObj), pageJs[i]);
                });
            });
        },
        getCurrentPageNames: function () {
            return _pageNames;
        },
        getMainPath: function () {
            return window.goobolTemplateDir;
        },
        addNewScriptFile: function (head, filePath) {
            var new_element = $("<script>");
            new_element.attr("type", "text/javascript");
            new_element.attr("src", filePath);
            $(head).append(new_element);
        },
        addNewLinkFile: function (head, filePath) {
            var new_element = $("<link>");
            new_element.attr("type", "text/css");
            new_element.attr("rel", "stylesheet");
            new_element.attr("href", filePath);
            $(head).append(new_element);
        },
        parsingGoobolUrls: function (obj, goobolUrls) { //goobolUrlsid2:src2}
            $(obj).find("[goobolurl]").each(function () {
                try {
                    var goobolUrlContent = $(this).attr("goobolurl");
                    var keyValue = goobolUrlContent.split('-');
                    $(this).attr(keyValue[0], goobolUrls[keyValue[1]]);
                } catch (e) {
                }
            });
        },
        pageLoading :function () {
            var imgHtml = "<img style='clear:auto;display:block;margin:auto;' src='" + $.goobol.getMainPath() + "/images/loading.gif" + "'></img>";
            $(_parentObj).html(imgHtml);
        }
    };
    var _parentObj;
    var _pageNames; //muiltiple names split by comma
    var _config;
    function initialConfig() {
        _config = {
            "goobol_home": {
                "page": [$.goobol.getMainPath() + "/pages/goobol.com/HomePage.htm"],
                "js": [$.goobol.getMainPath() + "/jquery-slider/js/jssor.slider.mini.js", $.goobol.getMainPath() + "/pagejs/goobol.homepage.js"],
                "goobolUrlKeyValue": {
                    "goobolurlflag_1": $.goobol.getMainPath() + "/jquery-slider/img/1920/purple.jpg",
                    "goobolurlflag_2": $.goobol.getMainPath() + "/jquery-slider/img/1920/blue.jpg"
                }
            }
        };
    }
})(jQuery);
